package com.youan.serve.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youan.serve.controller.swagger.UserControllerSwagger;
import com.youan.serve.dao.domain.Log;
import com.youan.serve.service.IUserService;
import com.youan.serve.dao.domain.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;




import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Controller
@RequestMapping("/user")
public class UserController implements UserControllerSwagger{
    @Autowired
    private IUserService userService;

    @Override
    @PostMapping("/user")
    public User addUser(User user) throws Exception{
        if(userService.judgeUser(user)){
            user.setUserId(UUID.randomUUID().toString().replaceAll("-", ""));
            userService.save(user);
        }
        return null;
    }

    @Override
    @PutMapping("/user")
    public User updateUser(User user) throws Exception{
        if(userService.judgeUser(user)){
            userService.updateById(user);
        }
        return null;
    }

    @Override
    @GetMapping("/users")
    public Page<User> selectAllUser(Page<User> page){
        return userService.page(page);
    }

    @Override
    @DeleteMapping("/user/{userId}")
    public void deleteUserByUserId(@PathVariable String userId){
        userService.removeById(userId);
    }

    @Override
    @GetMapping("/user/{userId}")
    public User selectUserByUserId(@PathVariable String userId){
        return userService.getById(userId);
    }

    @Override
    @GetMapping("/user/conditions")
    public Page<User> getUsersByConditions(Page<User> page, String orgId, String userName, String realName, String roleId) throws Exception{
        List<User> userList = userService.getUsersByConditions(orgId, userName, realName, roleId);
        page.setTotal(userList.size());
        return page.setRecords(userList);
    }
}

